//
//  WaveLineView.swift
//  波形图demo
//
//  Created by apple on 2021/2/7.
//

import SwiftUI

struct wavcItemData {
    var timeX: CGFloat
    var uA: CGFloat
}

struct WaveLineView: View {
    
    @State var dataSouce:[wavcItemData]
    
    var timer = Timer.publish(every: 0.01, tolerance: 0.05, on: .main, in: .common).autoconnect()

    let list = [12,132,16,1234,34,45,567,46,576,57,59]
    var body: some View {
        GeometryReader { geo in
            VStack {
                Path(){ path in
                    
                    var beginX: CGFloat = 20
                    let beginY: CGFloat = 20
                    
                    //开始起点
                    path.move(to: CGPoint(x: beginX, y: beginY))

                    dataSouce.forEach { point in
                        path.addLine(to: CGPoint(x: beginX, y: point.uA))
                        beginX += 1
                    }

                }.stroke(Color.orange, lineWidth: 1.0)
                .animation(.easeIn, value: true)
                Text("Size:  (\(geo.size.width), \(geo.size.height))")
                
                Path(){ path in
                    
                    var beginX: CGFloat = 20
                    let beginY: CGFloat = 20
                    
                    //开始起点
                    path.move(to: CGPoint(x: beginX, y: beginY))

                    dataSouce.reversed().forEach { point in
                        path.addLine(to: CGPoint(x: beginX, y: point.uA))
                        beginX += 1
                    }

                }.stroke(Color.orange, lineWidth: 1.0)
                .animation(.easeIn, value: true)
                
                Path(){ path in
                    
                    var beginX: CGFloat = 20
                    let beginY: CGFloat = 20
                    
                    //开始起点
                    path.move(to: CGPoint(x: beginX, y: beginY))

                    dataSouce.reversed().forEach { point in
                        path.addLine(to: CGPoint(x: beginX, y: point.uA))
                        beginX += 1
                    }

                }.stroke(Color.orange, lineWidth: 1.0)
                .animation(.easeIn, value: true)
                
                Path(){ path in
                    
                    var beginX: CGFloat = 20
                    let beginY: CGFloat = 20
                    
                    //开始起点
                    path.move(to: CGPoint(x: beginX, y: beginY))

                    dataSouce.reversed().forEach { point in
                        path.addLine(to: CGPoint(x: beginX, y: point.uA))
                        beginX += 1
                    }

                }.stroke(Color.orange, lineWidth: 1.0)
                .animation(.easeIn, value: true)
                
                Path(){ path in
                    
                    var beginX: CGFloat = 20
                    let beginY: CGFloat = 20
                    
                    //开始起点
                    path.move(to: CGPoint(x: beginX, y: beginY))

                    dataSouce.reversed().forEach { point in
                        path.addLine(to: CGPoint(x: beginX, y: point.uA))
                        beginX += 1
                    }

                }.stroke(Color.orange, lineWidth: 1.0)
                .animation(.easeIn, value: true)
                
                Path(){ path in
                    
                    var beginX: CGFloat = 20
                    let beginY: CGFloat = 20
                    
                    //开始起点
                    path.move(to: CGPoint(x: beginX, y: beginY))

                    dataSouce.reversed().forEach { point in
                        path.addLine(to: CGPoint(x: beginX, y: point.uA))
                        beginX += 1
                    }

                }.stroke(Color.orange, lineWidth: 1.0)
                .animation(.easeIn, value: true)
                
                Path(){ path in
                    
                    var beginX: CGFloat = 20
                    let beginY: CGFloat = 20
                    
                    //开始起点
                    path.move(to: CGPoint(x: beginX, y: beginY))

                    dataSouce.reversed().forEach { point in
                        path.addLine(to: CGPoint(x: beginX, y: point.uA))
                        beginX += 1
                    }

                }.stroke(Color.orange, lineWidth: 1.0)
                .animation(.easeIn, value: true)
                
                Path(){ path in
                    
                    var beginX: CGFloat = 20
                    let beginY: CGFloat = 20
                    
                    //开始起点
                    path.move(to: CGPoint(x: beginX, y: beginY))

                    dataSouce.reversed().forEach { point in
                        path.addLine(to: CGPoint(x: beginX, y: point.uA))
                        beginX += 1
                    }

                }.stroke(Color.orange, lineWidth: 1.0)
                .animation(.easeIn, value: true)
                
                Path(){ path in
                    
                    var beginX: CGFloat = 20
                    let beginY: CGFloat = 20
                    
                    //开始起点
                    path.move(to: CGPoint(x: beginX, y: beginY))

                    dataSouce.reversed().forEach { point in
                        path.addLine(to: CGPoint(x: beginX, y: point.uA))
                        beginX += 1
                    }

                }.stroke(Color.orange, lineWidth: 1.0)
                .animation(.easeIn, value: true)
                
            }
            .onReceive(timer) { _ in

                let waveValue = Float.randomCGFloatNumber(lower: 0, upper: 100)
                print("\(waveValue)")
//                self.dataSouce.insert(wavcItemData.init(timeX: 20, uA: CGFloat(waveValue)), at: 0)
                self.dataSouce.append(wavcItemData.init(timeX: 20, uA: CGFloat(waveValue)))
                
                print("\(self.dataSouce)----\(self.dataSouce)")
                
                let array = list[5..<6]
                print(array)
                
            }
            .onChange(of: self.dataSouce.count, perform: { value in
                print("value------\(value)")
            })
        }
    }
}

public extension Float {
    static func randomCGFloatNumber(lower: Float = 0,
                                    upper: Float = 100) -> Float {
        return Float(arc4random()) / Float(UInt32.max) * (upper - lower) + lower
    }
}
